//===================================================================================
// $$
//
// Coder: Code Milker v1.0
// 0 - EntityName
// 1 - lower
//===================================================================================


namespace App.Data.SqlServer.Repositories {
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using App.Model;

    public class BulletinRepository : BaseRepository, IBulletinRepository {
        public BulletinRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(Bulletin bulletin) {
            this.GetDbSet<Bulletin>().Add(bulletin);
            this.UnitOfWork.SaveChanges();
        }

        public Bulletin GetBulletin(Guid bulletinId) {
            return this.GetDbSet<Bulletin>()
                .Where(c => c.Id == bulletinId)
                .Single();
        }

        public void Update(Bulletin updatedBulletin) {
            this.GetDbSet<Bulletin>().Attach(updatedBulletin);
            this.SetEntityState(updatedBulletin, updatedBulletin.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid bulletinId) {
            Bulletin BulletinToDelete = this.GetBulletin(bulletinId);
            this.GetDbSet<Bulletin>().Remove(BulletinToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IQueryable<Bulletin> Find()
        {
            return this.GetDbSet<Bulletin>();
        }

        public IQueryable<Bulletin> GetAll()
        {
            return this.GetDbSet<Bulletin>();
        }
    }
}
